<?php

namespace Admin\Controller;

// 视频管理
class VideoDailyRecommendController extends AdminController 
{

	public function index()
	{
		$month = I('month', date('Y-m')).'-01';
		$query = [
				['date' => ['$gte'=> $month] ],
				['date' => ['$lte'=> date('Y-m-t', strtotime($month))] ],
			];
		$map['$and'] = $query;
		$res = service('VideoDailyRecomment')->distinct('date', $map);
		foreach ($res as $key => $value) {
			$list[] = [
				'date' => $value,
				'count' => service('VideoDailyRecomment')->where(['date' => $value])->count(),
			];
		}
		$this->assign('list', $list);
		$this->assign('last', date('Y-m', strtotime("$month -1 month")));
		$this->assign('next', date('Y-m', strtotime("$month +1 month")));
		$this->display();
	}


	/** 
	 * [设置视频]
	 * @author Legend. <xcx_legender@qq.com>
	 * @param 
	 * @return 
	 */
	public function add($video_id)
	{
		$video = service('Video')->find( (int)$video_id);
		if (!$video)
		{
			$this->error('视频信息错误');
		}
		if (!IS_POST)
		{
			$this->assign('video', $video);
			$this->video_id = $video_id;
			return $this->display('set');
		}
		$data = [
			'video_id' => (int)$video_id,
			'date'     => I('date'),
		];
		if (service('VideoDailyRecomment')->where($data)->find())
		{
			return $this->error('该日已经添加了此视频, 请进入精选日期进行修改.');
		}
		$data['reason'] = I('reason','', 'htmlspecialchars');
		$data['sort']   = 0;
		$cover = I('cover', '', 'trim');
		$cover && $data['cover'] = $cover;
		$res = service('VideoDailyRecomment')->add($data);
		$res ? $this->success('添加成功, 随后进入精选日期管理排序', U('sort?date='.$data['date'])) : $this->error('添加失败');
	}

	public function edit($id = 0)
	{
		$map = ['id' => (int)$id];
		$info = service('VideoDailyRecomment')->where($map)->find();
		if (!$info)
		{
			$this->error('信息错误');
		}
		if (!IS_POST)
		{
			$video = service('Video')->find( (int)$info['video_id']);
			$this->assign($info);
			$this->assign('video', $video);
			return $this->display('set');
		}
		else
		{
			$save = ['date' => I('date'), 'reason' => I('reason','', 'htmlspecialchars')];

			$cover = I('cover', '', 'trim');
			$cover && $save['cover'] = $cover;
			$res = service('VideoDailyRecomment')->where($map)->save($save);
			$res ? $this->success('修改成功') : $this->error('修改失败');
		}
	}

	/** 
	 * [删除精选视频]
	 * @author Legend. <xcx_legender@qq.com>
	 * @param 
	 * @return 
	 */
	public function del($id = '')
	{
		$id = array_unique((array)I('id',0));
        $id = is_array($id) ? $id : [$id];
        if ( empty($id) ) {
            $this->error('请选择要操作的数据!');
        }
        $id = array_map('intval', $id);
        $map = ['id' => ['$in' => $id]];
        $res = service('VideoDailyRecomment')->where($map)->delete();
        $res ? $this->success('删除成功') : $this->error('删除失败');
	}

	/** 
	 * [进行视频排序]
	 * @author Legend. <xcx_legender@qq.com>
	 * @param 
	 * @return 
	 */
	public function sort($date)
	{
		$serv = service('VideoDailyRecomment');
		if (!IS_POST)
		{
			$map = [
				'date' => $date
			];
			$list = $serv->order('sort asc')->where($map)->select();
			foreach ($list as $key => &$value) {
				$video = service('Video')->where(['id' => $value['video_id']])->find();
				$value['title'] = $video['title'];
				$value['status'] = $video['status'];
			}
		 	$this->list = $list;
			return $this->display();
		}
		else
		{
			$sort = I('sort');
			// $map = ['date' => $date];
			foreach ($sort as $id => $sort) {
				$map['id'] = (int)$id;
				$save['sort'] = (int)$sort;
				$serv->where($map)->save($save);
			}
			$this->success('操作成功');
		}
	}

	/** 
	 * [清空日期视频]
	 * @author Legend. <xcx_legender@qq.com>
	 * @param 
	 * @return 
	 */
	public function clear($date = '')
	{
		$map = ['date' => $date];
		$res = service('VideoDailyRecomment')->where($map)->delete();
		$this->success('操作成功');
	}


}